(function (window,document) {
    'use strict';
    var key = 'order';

    function copy(target,source){
        for(var p in source){
            if(source.hasOwnProperty(p)){
                target[p] = source[p];
            }
        }
    }

    function save(data) {
        localStorage.setItem(key, JSON.stringify(data));
    }

    function remove() {
        localStorage.removeItem(key);
    }

    function get(k) {
        if(k && k!=key){
            key = k;
        }
        var json = localStorage.getItem(key);
        if (!json) return;
        return JSON.parse(json);
    }

    function clear() {
        localStorage.removeItem(key);
    }

    function initOrder(){
       return {
            userId:undefined,
            userName:undefined,
            storeId:undefined,
            tableId:undefined,
            tableName:undefined,
            state:0,
            people: 5,
            price: 0,
            total:0,
            originalPrice: 0,
            foods: []
        };
    }

    function _add(order,food){
        var price = food.discount == 10 ? food.price : food.price * food.discount * 0.1;
        price = _fixedOne(price);
        order.price += price;
        order.originalPrice += food.price;
        order.total += 1;

        order.price = _fixedOne(order.price);
        order.originalPrice = _fixedOne(order.originalPrice);
    }

    /**
     * 浮点数只有一位
     * @private
     */
    function _fixedOne(f) {
        var str = f.toFixed(1);
        return parseFloat(str);
    }

    function Cart(order) {
        if (order) {
            this.order = order;
        } else {
            this.order = initOrder();
            var data = get();
            if(data)
                copy(this.order,data);
        }

        if(this.order.count == 0){
            this.order.price = 0;
            this.order.originalPrice = 0;
        }

    }

    Cart.prototype.load = function(k){
        var data = get(k);
        this.order = initOrder();
        if(data)
            copy(this.order,data);
    }

    Cart.prototype.setOptions = function(opts){
        if(!opts) return;
        copy(this.order,opts);
    }

    Cart.prototype.tableName = function(tableName){
        if(tableName == undefined){
            return this.order.tableName;
        }else{
            this.order.tableName = tableName;
        }
    }

    Cart.prototype.tableId = function (tableId) {
        if(tableId === undefined){
            return this.order.tableId;
        }else{
            if(this.order.tableId != tableId){
                this.order.tableId = tableId;
            }
            
        }
    }

    Cart.prototype.storeId = function (storeId) {
        if(storeId === undefined){
            return this.order.storeId;
        }else{
            if(this.order.storeId != storeId){
                this.order.storeId = storeId;
            }
        }
    }

    Cart.prototype.originalPrice = function(){
        return this.order.originalPrice;
    }

    /**
     * 设置用户Id
     * @param userId
     */
    Cart.prototype.userId = function (userId) {
        if(userId === undefined){
            return this.order.userId;
        }else{
            this.order.userName = this.order.userId = userId;
        }

    }
    /**
     * 设置用餐人数
     * @param people
     */
    Cart.prototype.people = function(people){
        if(people == undefined){
            return this.order.people;
        }else{
            try{
                this.order.people = parseInt(people);
            }catch (error){
                this.order.people = 5;
            }
        }
    }
    /**
     * 添加以及存在的Item
     * @param menuId
     */
    Cart.prototype.addItemById = function (menuId) {
        var foods = this.order.foods;
        for(var i = 0;i < foods.length;i ++){
            var item = foods[i];
            if(item.menuId == menuId){
                item.count += 1;

                _add(this.order,item);
                this.save();
                return item.count;
            }
        }
    }

    Cart.prototype.addItem = function (food) {
        var foods = this.order.foods;
        for (var i = 0; i < foods.length; i++) {
            var item = foods[i];
            if (item.menuId == food.menuId) {
                item.count += 1;
                _add(this.order,item);
                this.save();
                return item.count;
            }
        }
        //未查找到，直接添加到列表
        _add(this.order,food);
        foods.push(food);
        this.save();
        return 1;
    }

    Cart.prototype.getPrice = function(){
        return this.order.price;
    }

    Cart.prototype.getItemCount = function (id) {
        var foods = this.order.foods;
        if(!foods || foods.length == 0) return 0;

        var count = 0;
        for(var i = 0;i < foods.length;i ++){
            if(foods[i].menuId == id){
                count = foods[i].count;
                break;
            }
        }

        return count;
    }

    Cart.prototype.getCount = function(){
        return this.order.total;
    }

    Cart.prototype.minusItem = function (id) {
        var foods = this.order.foods;
        for (var i = 0; i < foods.length; i++) {
            var item = foods[i];
            if (item.menuId == id) {
                item.count -= 1;
                var price = item.discount == 10 ? item.price : item.price * item.discount * 0.1;
                price = _fixedOne(price);
                this.order.price -= price;
                this.order.originalPrice -= item.price;
                this.order.total -= 1;
                if (item.count == 0) {
                    foods.splice(i, 1);
                }
                this.order.price = _fixedOne(this.order.price);
                this.order.originalPrice = _fixedOne(this.order.originalPrice);
                this.save();
                return item.count;
            }
        }
        return 0;
    }

    Cart.prototype.getOrder = function(){
        return this.order;
    }

    Cart.prototype.save = function () {
        save(this.order);
    }
    
    Cart.prototype.clear = function () {
        clear();
        this.order = initOrder();
    };
    var cart = new Cart();
    window.cart = cart;
}(window, document));
